package com.share.homestay.security;


import com.share.homestay.entity.SysUser;
import com.share.homestay.entity.TjPermissions;
import com.share.homestay.pojo.LoginUser;
import com.share.homestay.service.SysUserServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private SysUserServiceFeign userServiceFeign;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //通过用户名查询用户信息
        SysUser sysUser = userServiceFeign.getUserByUsername(username);
        //通过用户id获取菜单
        List<TjPermissions> perms = userServiceFeign.selectPermsByUserId(sysUser.getUserId());
        return new LoginUser(sysUser, new ArrayList<>(),perms);
    }
}
